<template>
	<view class="container">
		<!-- 顶部卡片 -->
		<view class="top-section">
			<view class="header">
				<text class="title">我的</text>
				<view class="setting" @click="toSetting">
					<image class="setting-icon"
						src="https://cdn8.axureshop.com/demo/1689476/images/%E6%88%91%E7%9A%84/u4801.png" />
				</view>
			</view>

			<view class="user-card">
				<view class="user-info">
					<image class="avatar" :src="msgData.avatar || defaultAvatar" />
					<view class="details">
						<view class="name-section">
							<text class="name">{{ msgData.name || msgData.nickname || '未命名' }}</text>
							
								<!-- <text>{{ member.name }}</text> -->

								<up-tag :text="RelationsSearchData.relationship" shape="circle" bgColor="#fa746b" size="mini" />
								<up-tag text="已签约" shape="circle" bgColor="#28d094" size="mini"
									style="margin-left: 10rpx" />
							
						</view>
						<view class="meta">
							<text>{{ msgData.gender || '男' }}</text>
							<text>{{ msgData.age || 28 }}岁</text>
						</view>
					</view>
				</view>
				<button class="switch-btn" @click="showPopup = true">切换家庭成员</button>
			</view>
		</view>

		<!-- 功能列表 -->
		<all-list />

		<!-- 家庭成员选择弹窗 -->
		<uni-popup ref="popup" type="bottom" :mask-click="true" :show="showPopup" @maskClick="showPopup = false">
			<view class="popup-container">
				<text class="popup-title">请选择签约人</text>
				<view class="member-list">
					<view v-for="member in RelationsSearchData" :key="member.ownId" class="member-item"
						@click="selectMember(member)">
						<text>{{ member.name }}</text>
					</view>
				</view>
				<button class="cancel-btn" @click="showPopup = false">取消</button>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		useUserStore
	} from '../../store/user';
	import {
		getRelationsSearchApi,
		getUserInfoApi
	} from '../../api/login';
	import {
		onLoad
	} from "@dcloudio/uni-app";
import { useContractStore } from '../../store/contract';
	// 状态机
	const userStore = useUserStore();
	const useContract=useContractStore()
	
	const defaultAvatar = 'https://placeholder.com/avatar'; // 默认头像
	const msgData = ref({});
	const RelationsSearchData = ref([]);
	const showPopup = ref(false);

console.log('kk',useContract.contractState);



	// 获取用户信息
	const getUserInfo = async () => {
		const res = await getUserInfoApi();
		if (res.code === 200) {
			msgData.value = res.data;
			console.log('用户信息:', res.data);
		}
	};

	// 获取家庭成员
	const getRelationsSearch = async () => {
		const res = await getRelationsSearchApi({
			ownId: userStore.userInfo._id
		});
		if (res.code === 200) {
			RelationsSearchData.value = res.data;
			console.log('家庭成员信息:', res.data);
		}
	};
	// 选择家庭成员
	const selectMember = (member) => {
		console.log('已选择:', member);
		showPopup.value = false;
	};

	// 跳转设置页
	const toSetting = () => {
		uni.navigateTo({
			url: '/minepackge/pages/mine-setting/mine-setting'
		});
	};

	onLoad(() => {
		getUserInfo();
		getRelationsSearch();
	});
</script>

<style lang="scss" scoped>
	.cardName {
		font-size: 20px;
		color: #fff;
		margin-right: 20rpx;
		width: 110rpx;
		/* 容器宽度 */
		white-space: nowrap;
		/* 文本不换行 */
		overflow: hidden;
		/* 超出部分隐藏 */
		text-overflow: ellipsis;
		/* 超出部分显示为省略号 */
	}

	.homeTeam {
		width: 200rpx;
		height: 64rpx;
		color: #2984f8;
		font-size: 12px;
		border-radius: 16px;
		margin-right: 40rpx;

	}

	/* 顶部区域 */
	.top-section {
		background: #fff;
		padding: 0 40rpx 60rpx;
		margin-bottom: 20rpx;
	}

	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 0;

		.title {
			font-size: 44rpx;
			font-weight: bold;
		}

		.setting-icon {
			width: 40rpx;
			height: 40rpx;
			// border-radius: 16px;
		}
	}

	/* 用户卡片 */
	.user-card {
		background: #2984f8;
		border-radius: 32rpx;
		padding: 40rpx;
		margin-top: 30rpx;
		position: relative;

	}

	.user-info {
		display: flex;
		align-items: center;

		.avatar {
			width: 128rpx;
			height: 128rpx;
			border-radius: 10px;
			margin-right: 40rpx;
		}

		.details {
			flex: 1;
		}

		.name-section {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;

			.name {
				font-size: 40rpx;
				color: #fff;
				margin-right: 20rpx;
				max-width: 200rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}

		.meta {
			font-size: 28rpx;
			color: rgba(255, 255, 255, 0.8);

			text {
				margin-right: 20rpx;
			}
		}
	}

	.switch-btn {
		position: absolute;
		right: 40rpx;
		bottom: 40rpx;
		width: 220rpx;
		height: 64rpx;
		line-height: 64rpx;
		background: rgba(255, 255, 255, 0.2);
		color: #fff;
		font-size: 24rpx;
		border-radius: 32rpx;
		border: none;
	}

	/* 弹窗样式 */
	.popup-container {
		background: #fff;
		border-radius: 40rpx 40rpx 0 0;
		padding: 40rpx;
	}

	.popup-title {
		font-size: 36rpx;
		font-weight: bold;
		display: block;
		text-align: center;
		margin-bottom: 40rpx;
	}

	.member-list {
		.member-item {
			padding: 30rpx 0;
			border-bottom: 1rpx solid #eee;
			text-align: center;
			font-size: 32rpx;

			&:active {
				background-color: #f5f5f5;
			}
		}
	}

	.cancel-btn {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		background: #f5f5f5;
		color: #333;
		font-size: 32rpx;
		border-radius: 40rpx;
		margin-top: 40rpx;
		border: none;
	}
</style>